﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Text;

using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
//using DTG.Html;
//using DTG.Converting;


namespace TotalSport
{
    public partial class SelectQuotes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
            spanMsg.InnerHtml = "";
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("LoginUser.aspx");
            }
            else if (Session["Location"].ToString() == "Admin")
            {
                ddllocation.Enabled = true;
            }
            else
            {
                ddllocation.SelectedValue = Convert.ToString(Session["Location"]);
                ddllocation.Enabled = false;

            }

            if (!IsPostBack)
            {
                BAL.Customers.PopulateLocation(ref ddllocation);
                getQuotesgdvw();
                ddllocation.BackColor = System.Drawing.Color.LightGray;
            }
            //  grdQuotes.SelectedRowStyle.BackColor = ConsoleColor.Black ;
        }
        protected void btneditinvoice_Click(object sender, EventArgs e)
        {
            Button link = (Button)sender;
            GridViewRow row = (GridViewRow)link.Parent.Parent;
            GridViewRow grallquote = (GridViewRow)(link.Parent.Parent);
            int keyref = Convert.ToInt32(grdQuotes.DataKeys[row.RowIndex]["refno"]);
            Response.Redirect("EditInvoice.aspx?refno=" + keyref);
        }

        public void getQuotesgdvw()
        {
            string loca = ddllocation.SelectedValue;
            DataSet ds = BAL.Customers.Getallquotes(loca);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    grdQuotes.DataSource = ds;
                    grdQuotes.DataBind();
                }
                else
                    grdQuotes.DataBind();
            }
        }

        protected void grdQuotes_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {


        }

        protected void grdQuotes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdQuotes.PageIndex = e.NewPageIndex;
            getQuotesgdvw();
        }

        //public void calculateprice(decimal dsctrate)
        //{

        //    int totalprice = Convert.ToInt32(lblTotalPrice.Text);
        //    decimal finalprice = Convert.ToDecimal(totalprice + (totalprice * dsctrate / 100));
        //    lblVCharge.Text = (totalprice * 12.5 / 100).ToString() + "(" + dsctrate + "%)";
        //    lblFinalPrice.Text = finalprice.ToString();

        //}

        protected void lnkrefID_Click(object sender, EventArgs e)
        {



            LinkButton lnkid = (LinkButton)sender;
            //GridViewRow gr = (GridViewRow)lnkid.Parent;

            //  int quoteId = Convert.ToInt32(grdQuotes.DataKeys[gr.RowIndex]["QuoteID"]);

            int refid = Convert.ToInt32(lnkid.Text);
            Session.Add("refno", refid);

            //  txtDsctRate.Text = a;   


            string[] details = BAL.Customers.getquotepersonald(refid);
            DataSet ds = BAL.Customers.getQutes(refid);

            rptQuot.DataSource = ds;
            rptQuot.DataBind();
            //  int val = BAL.Customers.Calculatefinalprice(refid);
            // lblTotalPrice.Text = val.ToString();
            // lblSubid.Value = quoteId.ToString();
            lblrefeno.Text = details[1];
            lblName.Text = details[3];
            lblcompanyname.Text = details[6];
            lblcreatedby.Text = details[12];


            string[] detl = BAL.Customers.getNotes(refid);
            string subCtgry = BAL.Customers.getSubcateryName(refid);

            if (subCtgry == "Fitness")
            {
                lblVATi.Text = "10.7% VAT will be charged extra";
                decimal a = Convert.ToDecimal(10.7);
                //   calculateprice(a);

            }
            else if (subCtgry == "Game")
            {

                lblVATi.Text = "12.5% VAT will be charged extra";
                decimal a = Convert.ToDecimal(12.5);
                // calculateprice(a);
            }

            else if (subCtgry == "RECREATIONAL")
            {

                lblVATi.Text = "15.2% VAT will be charged extra";
                decimal a = Convert.ToDecimal(15.2);
                // calculateprice(a);
            }

            if (detl[2] == true.ToString())
            {
                //  tr1.Style.Add("display", "block");
                // tr2.Style.Add("display", "block");
                //tr3.Style.Add("display", "block");


            }
            if (detl[2] == false.ToString())
            {
                lblVAT.Style.Add("display", "none");

                // tr3.Style.Add("display", "block");
                //  lblFinalPrice.Text = val.ToString();
            }

            if (detl[3] == false.ToString())
            {
                lblAdvance.Style.Add("display", "none");

            }
            if (detl[4] == false.ToString())
            {
                lbldelivery.Style.Add("display", "none");

            }
            if (detl[5] == false.ToString())
            {
                urgentlbl.Style.Add("display", "none");

            }
            if (detl[6] == false.ToString())
            {
                lblCheq.Style.Add("display", "none");

            }
            if (detl[7] == false.ToString())
            {
                lblvalidityquote.Style.Add("display", "none");

            }
            if (detl[8] == false.ToString())
            {
                lbloctroi.Style.Add("display", "none");

            }

            lnkView_ModalPopupExtender.Show();
        }
        protected void rptQuot_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            Label lblsrn = (Label)e.Item.FindControl("lblsrno");
            // RepeaterItem rptsr = (RepeaterItem)lblsrn.Parent;
            lblsrn.Text = (e.Item.ItemIndex + 1).ToString();
        }

        //protected void btnSubmit_Click(object sender, EventArgs e)
        //{
        //    Button submit = (Button)sender;

        //    RepeaterItem rptsr = (RepeaterItem)submit.Parent;

        //    Label lblPricce = (Label)rptsr.FindControl("lblPrice");
        //    HtmlInputHidden lblID = (HtmlInputHidden)rptsr.FindControl("lblSubid");
        //    TextBox txtrate = (TextBox)rptsr.FindControl("txtdsctamt");

        //    decimal dsctrate = Convert.ToInt32(txtrate.Text);
        //    int oldprice = Convert.ToInt32(lblPricce.Text);

        //    decimal newprice = Convert.ToDecimal(oldprice - oldprice * (dsctrate / 100));
        //    int id = Convert.ToInt32(lblID.Value);

        //    BAL.Customers.UpdatedsctPrice(dsctrate, newprice, id);

        //    DataSet ds = BAL.Customers.getQutes(Convert.ToInt32(Session["refno"]));

        //    rptQuot.DataSource = ds;
        //    rptQuot.DataBind();

        //    int val = BAL.Customers.Calculatefinalprice(Convert.ToInt32(Session["refno"]));
        //    //lblTotalPrice.Text = val.ToString();
        //    //int totalprice = Convert.ToInt32(lblTotalPrice.Text);
        //    //decimal finalprice = Convert.ToDecimal(totalprice + (totalprice * 12.5 / 100));
        //    //lblVCharge.Text = (totalprice * 12.5 / 100).ToString();


        //    //lblFinalPrice.Text = finalprice.ToString();
        //    // lblVCharge.Text = ( Convert.ToDecimal( lblTotalPrice.Text) * 12.5 / 100).ToString();
        //    // lblFinalPrice.Text = finalprice.ToString();
        //    lnkView_ModalPopupExtender.Show();
        //}

        protected void btnPrintInvoice_Click2(object sender, EventArgs e)
        {



        }

        protected void btcalculate_Click(object sender, EventArgs e)
        {
            int refno = Convert.ToInt32(Session["refno"]);

            DataSet ds = BAL.Customers.getQutes(Convert.ToInt32(Session["refno"]));
            rptQuot.DataSource = ds;
            rptQuot.DataBind();
            lnkView_ModalPopupExtender.Show();
        }

        protected void grdQuotes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton edit = (ImageButton)e.Row.Cells[0].FindControl("edit");
                edit.CommandArgument = e.Row.RowIndex.ToString();


                Button btnekdit = (Button)e.Row.Cells[6].FindControl("btneditinvoice");

                Button btncreate = (Button)e.Row.Cells[6].FindControl("btninvoicecre");

                LinkButton lnkinvoice = (LinkButton)e.Row.FindControl("lnkrefID");
                HyperLink imgpdf = (HyperLink)e.Row.Cells[7].FindControl("imgpdf");
                imgpdf.NavigateUrl = "http://host2unlimited.in/total1/PDF/Quote-" + lnkinvoice.Text + ".pdf";

                HtmlAnchor acnor = (HtmlAnchor)e.Row.FindControl("invoice");
                Label invoi = (Label)e.Row.FindControl("invo");
                                string vall = invoi.Text;
                
                if (vall == "False")
                {

                    btnekdit.Visible = true;
                    btncreate.Visible = false;
                     
                    
                }
                else if (vall == "True")
                {
                    btnekdit.Visible = false;
                    
                    btncreate.Visible = true;
                   

                    //acnor.HRef = "PDF/" + acnor.Title + ".html";

                }



            }
        }

        protected void edit_Click(object sender, ImageClickEventArgs e)
        {

            ImageButton link = (ImageButton)sender;


            GridViewRow row = (GridViewRow)link.Parent.Parent;
            GridViewRow grallquote = (GridViewRow)(link.Parent.Parent);
            int keyref = Convert.ToInt32(grdQuotes.DataKeys[row.RowIndex]["refno"]);

            Response.Redirect("CreateQuote.aspx?refno=" + keyref);


            //ImageButton link = (ImageButton)sender;

            //GridViewRow grdRow = (GridViewRow)(link.Parent.Parent);

            //ImageButton OrderIDLink = (ImageButton)grdRow.FindControl("edit");
            //int RowIndex = Convert.ToInt32(OrderIDLink.CommandArgument);

            //int userid = Convert.ToInt32(grdQuotes.DataKeys[RowIndex]["refno"]);



        }

        protected void ddllocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            getQuotesgdvw();

        }

        protected void imgpdf_Click(object sender, ImageClickEventArgs e)
        {
            //Button link = (Button)sender;
            //GridViewRow row = (GridViewRow)link.Parent.Parent;
            //GridViewRow grallquote = (GridViewRow)(link.Parent.Parent);
            //int keyref = Convert.ToInt32(grdQuotes.DataKeys[row.RowIndex]["refno"]);
            //Response.Redirect("Invoice.aspx?refno=" + keyref);
        }

        protected void btninvoicecre_Click(object sender, EventArgs e)
        {
            Button link = (Button)sender;
            GridViewRow row = (GridViewRow)link.Parent.Parent;
            GridViewRow grallquote = (GridViewRow)(link.Parent.Parent);
            int keyref = Convert.ToInt32(grdQuotes.DataKeys[row.RowIndex]["refno"]);
            Response.Redirect("Invoice.aspx?refno=" + keyref);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string searchvalue = rblsearch.SelectedValue;
           
            string searchdata = txtSearch.Text;
            //if (searchvalue == "-1")
            //{
            //    getQuotesgdvw();
            //}
            if (searchvalue == "Quote ID")
            {
                SearchData(searchdata, "", "");

            }
            else if (searchvalue == "Recipient Name")
            {
                SearchData("", searchdata, "");
            }
            else if (searchvalue == "Company Name")
            {
                SearchData(searchdata, "", searchdata);
            }           

        }

        public void SearchData(string id, string name, string cname)
        {

            DataSet ds = BAL.Customers.ShowSearchedData(id, name, cname);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    grdQuotes.Visible = true;
                    grdQuotes.DataSource = ds;
                    grdQuotes.DataBind();
                }
                else
                {
                    spanMsg.Style.Add("display", "block");
                    spanMsg.InnerHtml = "NO Data Found";
                    grdQuotes.Visible = false;
                    
                }
            }
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }


        //protected void imgexcel_Click(object sender, ImageClickEventArgs e)
        //{
        //    // Read the specified html file.  
        //    HtmlDoc html = HtmlDoc.ReadHTML(@"77.html");

        //    // Convert Html table to Excel  
        //    HtmlToExcel htmlexcel = new HtmlToExcel();
        //    ExcelWorkbook excel = htmlexcel.Convert(html);

        //    //Add text and save Excel file as XLSX format.  
        //    excel.Worksheets[0].Cells["A1"].Value = "HTML to Excel";
        //    excel.WriteXLSX(@"..\..\..\Export_Html.xlsx");

        //    //Open specified file in MS Excel.  
        //    System.Diagnostics.Process.Start(@"..\..\..\Export_Html.xlsx");
        //    //}}
        //}





    }
}